layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 任务记录管理
     */
    var TaskRecord = {
        tableId: "taskRecordTable"
    };

    /**
     * 初始化表格的列
     */
    TaskRecord.initColumn = function () {
        return [[
            {field: 'taskRecordId', hide: true, title: '任务记录ID'},
            {field: 'taskId', sort: true, title: '任务ID'},
            {field: 'title', sort: true, title: '标题'},
            // {field: 'remark', sort: true, title: '备忘'},
            {field: 'type', sort: true, title: '类型', templet:function (d) {
                    if (d.type === 1) {
                        return  '<span class="layui-badge">日计划</span>';
                    } else if (d.type === 2) {
                        return  '<span class="layui-badge layui-bg-cyan">周计划</span>';
                    } else if (d.type === 3) {
                        return  '<span class="layui-badge layui-bg-green">月计划</span>';
                    } else {
                        return  '<span class="layui-badge layui-bg-red">普通任务</span>';
                    }
                }},
            {field: 'status', sort: true, title: '执行状态', templet:function (d) {
                    if (d.status === 0) {
                        return  '<span class="layui-badge">未执行</span>';
                    } else if (d.status === 1) {
                        return  '<span class="layui-badge layui-bg-cyan">执行完成</span>';
                    } else if (d.status === 2) {
                        return  '<span class="layui-badge layui-bg-green">执行取消</span>';
                    } else {
                        return  '<span class="layui-badge layui-bg-cyan">未执行</span>';
                    }
                }},
            {field: 'reviewStatus', sort: true, title: '审核状态', templet:function (d) {
                    if (d.reviewStatus === 0) {
                        return  '<span class="layui-badge">未审核</span>';
                    } else if (d.reviewStatus === 1) {
                        return  '<span class="layui-badge layui-bg-cyan">审核完成</span>';
                    } else if (d.reviewStatus === 2) {
                        return  '<span class="layui-badge layui-bg-green">审核取消</span>';
                    } else {
                        return  '<span class="layui-badge layui-bg-cyan">未审核</span>';
                    }
                }},
            {field: 'worker', sort: true, title: '执行人'},
            {field: 'reviewWorker', sort: true, title: '审核人'},
            {field: 'reportDate', sort: true, title: '执行时间'},
            {field: 'updateTime', sort: true, title: '更新时间'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TaskRecord.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(TaskRecord.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    TaskRecord.openAddDlg = function () {
        func.open({
            title: '添加任务记录',
            content: Feng.ctxPath + '/api/taskRecord/add',
            tableId: TaskRecord.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    TaskRecord.openEditDlg = function (data) {
        func.open({
            title: '修改任务记录',
            content: Feng.ctxPath + '/api/taskRecord/edit?taskRecordId=' + data.taskRecordId,
            tableId: TaskRecord.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    TaskRecord.exportExcel = function () {
        var checkRows = table.checkStatus(TaskRecord.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TaskRecord.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/taskRecord/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TaskRecord.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("taskRecordId", data.taskRecordId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var taskParam = "";
    if (!Feng.isUndefined(Feng.getUrlParam("taskId"))) {
        taskParam = '?taskId=' + Feng.getUrlParam("taskId");
    }
    var tableResult = table.render({
        elem: '#' + TaskRecord.tableId,
        url: Feng.ctxPath + '/api/taskRecord/list' +　taskParam,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TaskRecord.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TaskRecord.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TaskRecord.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TaskRecord.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TaskRecord.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TaskRecord.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TaskRecord.onDeleteItem(data);
        }
    });
});
